package booking.misc;

import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class User {

	private int userId;
	private String userName;
	private String userPassword;
	private String userEmail;
	private boolean admin;

	public User(int id, String name, String password, String email,
			boolean admin) {
		userId = id;
		userName = name;
		userPassword = password;
		userEmail = email;
		this.admin = admin;
	}

	public User(ActionListener actionListener) {
		// TODO Auto-generated constructor stub
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public boolean getAdmin() {
		return admin;
	}

	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	public String toString() {
		return userName + "," + userPassword + "," + admin;

	}

	public boolean dbInsert() {
		String sql = "INSERT INTO users (NAME,Password, Email, Admin) "
				+ "VALUES (?, ?, ?, ?)";
		try (Connection conn = DBConnectionData.getConnection()) {
			PreparedStatement statement = conn.prepareStatement(sql,
					PreparedStatement.RETURN_GENERATED_KEYS);
			statement.setString(1, userName);
			statement.setString(2, userPassword);
			statement.setString(3, userEmail);
			statement.setBoolean(4, admin);
			statement.executeUpdate();
			ResultSet keys = statement.getGeneratedKeys();
			if (keys.next()) {
				userId = keys.getInt(1);
			}
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
			return false;
		}
	}

	public boolean dbUpdate() {
		String sql = "UPDATE users SET NAME=?, Password=?, Email=?, Admin=? "
				+ "WHERE ID=?";
		try (Connection conn = DBConnectionData.getConnection()) {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setString(1, userName);
			statement.setString(2, userPassword);
			statement.setString(3, userEmail);
			statement.setBoolean(4, admin);
			statement.setInt(5, userId);
			
			statement.executeUpdate();
			
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
			return false;
		}
	}

	public boolean dbDelete() {
		String sql = "DELETE FROM users WHERE ID=?";
		try (Connection conn = DBConnectionData.getConnection()) {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setInt(1, userId);
			statement.executeUpdate();
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
			return false;
		}
	}

	public static User getUser(String userName, String password) {
		// JDBC-kod f���r att hitta anv���ndaren.
		String sql = "SELECT * FROM users WHERE name = ? AND password = ?";
		try (Connection conn = DBConnectionData.getConnection()) {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setString(1, userName);
			statement.setString(2, password);
			ResultSet rs = statement.executeQuery();

			if (rs.next()) {

				User user = new User(
						rs.getInt("id"),
						userName,
						password,
						rs.getString("email"),
						rs.getBoolean("admin")
						);

				return user;
			}

		} catch (SQLException ex) {
			ex.printStackTrace();
			System.out.println("Fel");
		}
		return null;
	}
}